/**
 * 
 */
package org.gtiles.components.login.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.gtiles.components.login.web.DefaultLoginJsonRenderImpl;
import org.gtiles.components.login.web.ILoginJsonRender;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.util.StringUtils;

/**
 * 通过URL中的“login_ct”参数进行判断。
 * <code>if (login_ct != null && login_ct.equals("ajax"))</code>
 * ,仅返回登录成功的json，反之按spring security原有逻辑跳转相关的URL请求<br>
 * 1、可以通过修改系统参数中的“plugins_login_jsonRenderBean”来扩展“login_ct”请求返回的json内容；<br>
 * 2、可以通过修改系统参数中的“plugins_loginFailure_postActionBean”来扩展完成登录失败后的业务处理；<br>
 * 3、可以通过修改系统参数中的“plugins_login_defaultFailureUrl”来指定是否登录失败后跳转到的路径；<br>
 * 4、可以通过修改系统参数中的“plugins_login_forwardToDestination”来指定通过forward形式跳转到登录失败路径；<br>
 * <br>
 * 
 * @author GuoR
 * @createDate 2016年3月18日
 */
public class CustomAuthenticationFailureHandler extends
		SimpleUrlAuthenticationFailureHandler {
	private String targetUrlParameter = "login_ct";
	@Override
	public void onAuthenticationFailure(HttpServletRequest request,
			HttpServletResponse response, AuthenticationException exception)
			throws IOException, ServletException {
		initParam();
		// 获取登录方式参数
		String clientType = request.getParameter(targetUrlParameter);
//		String postActionBean = (String) ConfigHolder.getConfigValue(
//				ComponentPlugin.PLUGIN_CODE,
//				"plugins_login_failurePostActionBean");
//		String jsonRenderBean = (String) ConfigHolder.getConfigValue(
//				ComponentPlugin.PLUGIN_CODE, "plugins_login_jsonRenderBean");
//		Assert.notNull(jsonRenderBean, "jsonRenderBean cannot be null");
		// 登录失败后进行的业务处理
//		if (!StringUtils.isEmpty(postActionBean)) {
//			String[] postActionArr = postActionBean.split(",");
//			for(int i =0,j=postActionArr.length; i<j; i++){
//				IPostHandlerAction postAction = (IPostHandlerAction) SpringBeanUtils
//						.getBean(postActionArr[i]);
//				postAction.loginPostHandler(request, response);
//			}
//		}
//		if(exception instanceof LoginCaptchaException){
//			request.setAttribute("captchafail", "false");
//		}
		if (StringUtils.hasText(clientType) && clientType.equals("ajax")) {
			super.saveException(request, exception);
			try {
				//FIXME 暂时写死实现类，下次迭代将它变成可配置的
				ILoginJsonRender jsonRender = new DefaultLoginJsonRenderImpl();
				jsonRender.loginFailure(request, response);
			} catch (Exception e) {
				throw new RuntimeException("登录json格式数据处理类实例化失败，请检查相关参数配置及接口实现是否正确。",e);
			}
		} else {
			super.onAuthenticationFailure(request, response, exception);
		}
	}
	protected void initParam() {
//		String defaultFailureUrl = (String) ConfigHolder
//				.getConfigValue(
//						ComponentPlugin.PLUGIN_CODE,
//						"plugins_login_defaultFailureUrl");
//		if (StringUtils.hasText(defaultFailureUrl)) {
//			super.setDefaultFailureUrl(defaultFailureUrl);
//		}
//		String forwardToDestination = (String) ConfigHolder
//				.getConfigValue(
//						ComponentPlugin.PLUGIN_CODE,
//						"plugins_login_forwardToDestination");
//		if (StringUtils.hasText(forwardToDestination)) {
//			boolean forwardToDestinationBoolean = false;
//			try {
//				forwardToDestinationBoolean = BooleanUtils
//						.parseBoolean(forwardToDestination);
//			} catch (IllegalArgumentException e) {
//				logger.error(e.getMessage(), e);
//			}
//			super.setUseForward(forwardToDestinationBoolean);
//		}
//		String targetUrlParameter = (String) ConfigHolder
//				.getConfigValue(
//						ComponentPlugin.PLUGIN_CODE,
//						"plugins_login_targetUrlParameter");
		String targetUrlParameter = "login_ct";
		if (StringUtils.hasText(targetUrlParameter)) {
			this.targetUrlParameter = targetUrlParameter;
		}
	}
}
